package com.ruoyi.project.cardbox.orders.controller;

import java.util.List;

import com.ruoyi.project.cardbox.orders.domain.OrdersBO;
import com.ruoyi.project.cardbox.orders.domain.OrdersVO;
import com.ruoyi.project.cardbox.request.domain.OrderRequest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cardbox.orders.domain.Orders;
import com.ruoyi.project.cardbox.orders.service.IOrdersService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 主订单Controller
 * 
 * @author ruoyi
 * @date 2025-07-04
 */
@Controller
@RequestMapping("/cardbox/orders")
public class OrdersController extends BaseController
{
    private String prefix = "cardbox/orders";

    @Autowired
    private IOrdersService ordersService;

    @RequiresPermissions("cardbox:orders:view")
    @GetMapping()
    public String orders()
    {
        return prefix + "/orders";
    }

    /**
     * 查询主订单列表
     */
//    @RequiresPermissions("cardbox:orders:list")
//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(Orders orders)
//    {
//        startPage();
//        List<Orders> list = ordersService.selectOrdersList(orders);
//        return getDataTable(list);
//    }

    @RequiresPermissions("cardbox:orders:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo listBO(Orders orders){
        startPage();
        List<OrdersBO> list = ordersService.selectOrdersBOList(orders);
        return getDataTable(list);
    }
    /**
     * 导出主订单列表
     */
    @RequiresPermissions("cardbox:orders:export")
    @Log(title = "主订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult exportBO(Orders orders)
    {
        List<OrdersBO> list = ordersService.selectOrdersBOList(orders);
        ExcelUtil<OrdersBO> util = new ExcelUtil<OrdersBO>(OrdersBO.class);
        return util.exportExcel(list, "主订单数据");
    }

    /**
     * 导出主订单列表
     */
//    @RequiresPermissions("cardbox:orders:export")
//    @Log(title = "主订单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    @ResponseBody
//    public AjaxResult export(Orders orders)
//    {
//        List<Orders> list = ordersService.selectOrdersList(orders);
//        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
//        return util.exportExcel(list, "主订单数据");
//    }

    /**
     * 新增主订单
     */
    @RequiresPermissions("cardbox:orders:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存主订单
     */
    @RequiresPermissions("cardbox:orders:add")
    @Log(title = "主订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Orders orders)
    {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改主订单
     */
    @RequiresPermissions("cardbox:orders:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Orders orders = ordersService.selectOrdersById(id);
        mmap.put("orders", orders);
        return prefix + "/edit";
    }

    /**
     * 修改保存主订单
     */
    @RequiresPermissions("cardbox:orders:edit")
    @Log(title = "主订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrdersVO ordersVO)
    {
        return toAjax(ordersService.updateOrders(ordersVO));
    }

    /**
     * 修改主订单状态
     */
    @RequiresPermissions("cardbox:orders:edit")
    @Log(title = "主订单", businessType = BusinessType.UPDATE)
    @PutMapping("/editStatus/{id}")
    @ResponseBody
    public AjaxResult editStatus(@PathVariable("id") Long id)
    {
        return toAjax(ordersService.updateOrdersStatus(id));
    }


    /**
     * 删除主订单
     */
    @RequiresPermissions("cardbox:orders:remove")
    @Log(title = "主订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }
}
